package tree.递归;

import po.TreeNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName N二叉树的直径543.java
 * @Description https://leetcode-cn.com/problems/diameter-of-binary-tree/
 * @createTime 2021年06月10日 19:35:00
 */
public class N二叉树的直径543 {

    int deep=0;
    public int diameterOfBinaryTree(TreeNode root) {
        //深度优先
        if (root == null) {
            return 0;
        }
        deep(root);
        return deep;
    }

    private int deep(TreeNode node) {
        //做选择
        if (node == null) {
            return 0;
        }
        //移除选择列表
        node= node.left;
        //进入下一层
        int leftDeep=deep(node);
        node=node.right;
        int rightDeep=deep(node);
        deep=Math.max(deep,rightDeep+leftDeep);
        return Math.max(leftDeep,rightDeep)+1;
    }
}
